package com.sskj.lib.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.MovementMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.R;
import com.zzhoujay.richtext.RichText;

public class TipUtil {

    private static boolean isSee;

    public static MaterialDialog getTip(Context context, String content, OnTipSure onTipSure) {
        return getTip(context, content, App.INSTANCE.getString(R.string.lib_tipUtil1_j), onTipSure);
    }

    public static MaterialDialog getTip(Context context, String content, boolean isCancel, OnTipSure onTipSure) {
        return new MaterialDialog.Builder(context)
                .title(App.INSTANCE.getString(R.string.lib_appDirectCheckUtil2_j))
                .content(content)
                .cancelable(false)
                .autoDismiss(false)
                .positiveText(App.INSTANCE.getString(R.string.lib_tipUtil1_j))
//                .negativeText("")
                .onAny((dialog, which) -> {
                    if (which == DialogAction.POSITIVE) {
                        dialog.dismiss();
                        onTipSure.onSure();
                    } else if (which == DialogAction.NEGATIVE) {
                        dialog.dismiss();
                    }
                }).build();
    }

    public static MaterialDialog getTip(Context context, String content, String sureMsg, OnTipSure onTipSure) {
        return new MaterialDialog.Builder(context)
                .title(App.INSTANCE.getString(R.string.lib_appDirectCheckUtil2_j))
                .content(content)
                .positiveText(sureMsg)
                .negativeText(App.INSTANCE.getString(R.string.lib_tipUtil3_j))
                .onAny((dialog, which) -> {
                    if (which == DialogAction.POSITIVE) {
                        dialog.dismiss();
                        onTipSure.onSure();
                    } else if (which == DialogAction.NEGATIVE) {
                        dialog.dismiss();
                    }
                }).build();
    }

    public static MaterialDialog getTip(Context context, String content, String sureMsg, OnTipSure2 onTipSure) {
        return new MaterialDialog.Builder(context)
                .title(App.INSTANCE.getString(R.string.lib_appDirectCheckUtil2_j))
                .content(content)
                .positiveText(sureMsg)
                .negativeText(App.INSTANCE.getString(R.string.lib_tipUtil3_j))
                .onAny((dialog, which) -> {
                    if (which == DialogAction.POSITIVE) {
                        dialog.dismiss();
                        onTipSure.onSure();
                    } else if (which == DialogAction.NEGATIVE) {
                        dialog.dismiss();
                        onTipSure.onCancel();
                    }
                }).build();
    }

    public static MaterialDialog getTip(Context context, String title, String content, String sureMsg, OnTipSure onTipSure) {
        return new MaterialDialog.Builder(context)
                .title(title)
                .content(content)
                .positiveText(sureMsg)
                .negativeText(App.INSTANCE.getString(R.string.lib_tipUtil3_j))
                .onAny((dialog, which) -> {
                    if (which == DialogAction.POSITIVE) {
                        dialog.dismiss();
                        onTipSure.onSure();
                    } else if (which == DialogAction.NEGATIVE) {
                        dialog.dismiss();
                    }
                }).build();
    }

    public static void showGonggao(Context context, String content) {
        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.lib_dialog_gonggao, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();
        View customeView = dialog.getCustomView();

        ((TextView) customeView.findViewById(R.id.tvContent)).setText(content);
        ClickUtil.click(customeView.findViewById(R.id.btSure), () -> {
            dialog.dismiss();
        });
    }

    public static void showAward(Context context, String content, String num) {
        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.lib_dialog_award, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();
        View customView = dialog.getCustomView();
        ((ViewGroup) (customView.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((TextView) customView.findViewById(R.id.tvContent)).setText(content);
        ((TextView) customView.findViewById(R.id.cqtf_num)).setText(num);
        ((LinearLayout) customView.findViewById(R.id.ll_show)).setVisibility(TextUtils.isEmpty(num) ? View.GONE : View.VISIBLE);
        ClickUtil.click(customView.findViewById(R.id.btSure), () -> {
            dialog.dismiss();
        });
    }

    public static void showAward(Context context, String content, OnTipSure onTipSure) {
        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.lib_dialog_award, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();
        View customView = dialog.getCustomView();
        ((ViewGroup) (customView.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((TextView) customView.findViewById(R.id.tvContent)).setText(content);
        ClickUtil.click(customView.findViewById(R.id.btSure), () -> {
            dialog.dismiss();
            onTipSure.onSure();
        });
    }

    public static MaterialDialog showPwd(Context context, OnInput onInput) {
        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.lib_dialog_pwd, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();
        View customView = dialog.getCustomView();

        ((ViewGroup) (customView.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        EditText etPwd = customView.findViewById(R.id.etPwd);
        ImageView ivPwdToggle = customView.findViewById(R.id.ivPwdToggle);
        isSee = false;
        ClickUtil.click(200, ivPwdToggle, () -> {
            isSee = !isSee;
            etPwd.setTransformationMethod(isSee ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
            ivPwdToggle.setImageResource(isSee ? R.mipmap.lib_icon_show : R.mipmap.lib_icon_hide);
        });
        ClickUtil.click(customView.findViewById(R.id.btCancel), () -> {
            dialog.dismiss();
        });
        ClickUtil.click(customView.findViewById(R.id.btSure), () -> {
            if (TextUtils.isEmpty(etPwd.getText())) {
                ToastUtil.showShort("请输入密码");
                return;
            }
            dialog.dismiss();
            onInput.onInput(etPwd.getText().toString());
        });
        return dialog;
    }


    public static MaterialDialog getExitTip(Activity activity, String content, OnTipSure onTipSure) {
        MaterialDialog dialog = new MaterialDialog.Builder(activity)
                .customView(R.layout.lib_dialog_exit, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();
        View customView = dialog.getCustomView();
        TextView tvContent = customView.findViewById(R.id.tvContent);
        tvContent.setText(content);
        ClickUtil.click(dialog.getCustomView().findViewById(R.id.btSure), () -> {
            onTipSure.onSure();
        });
        return dialog;
    }

    public static MaterialDialog getSureHtmlTip(Activity activity, String title, String content, OnTipSure onTipSure) {
        MaterialDialog dialog = new MaterialDialog.Builder(activity)
                .customView(R.layout.lib_dialog_sure, false)
                .autoDismiss(true)
                .cancelable(true)
                .show();
        View customView = dialog.getCustomView();
        TextView tvContent = customView.findViewById(R.id.tvContent);
        TextView tvTitle = customView.findViewById(R.id.tvTitle);
        RichText.from(content).into(tvContent);
        tvContent.setMovementMethod(ScrollingMovementMethod.getInstance());
        tvContent.setText(content);
        tvTitle.setText(title);
        ClickUtil.click(dialog.getCustomView().findViewById(R.id.btSure), () -> {
            onTipSure.onSure();
        });
        return dialog;
    }

    public static MaterialDialog getSureTip(Activity activity, String title, String content, OnTipSure onTipSure) {
        MaterialDialog dialog = new MaterialDialog.Builder(activity)
                .customView(R.layout.lib_dialog_sure, false)
                .autoDismiss(true)
                .cancelable(true)
                .show();
        View customView = dialog.getCustomView();
        TextView tvContent = customView.findViewById(R.id.tvContent);
        RichText.from(content).into(tvContent);

        TextView tvTitle = customView.findViewById(R.id.tvTitle);
//        tvContent.setMovementMethod(ScrollingMovementMethod.getInstance());
        tvContent.setText(content);
        tvTitle.setText(title);
        ClickUtil.click(dialog.getCustomView().findViewById(R.id.btSure), () -> {
            onTipSure.onSure();
        });
        return dialog;
    }

    public static MaterialDialog getSureTip(Activity activity, String content, OnTipSure onTipSure) {
        return getSureTip(activity, App.INSTANCE.getString(R.string.lib_appDirectCheckUtil2_j), content, onTipSure);
    }

    public static MaterialDialog getSureCancelTip(Activity activity, String content, OnTipSure onTipSure) {
        return getSureCancelTip(activity, App.INSTANCE.getString(R.string.lib_appDirectCheckUtil2_j), content, onTipSure);
    }

    public static MaterialDialog getSureCancelTip(Activity activity, String title, String content, OnTipSure onTipSure) {
        return getSureCancelTip(activity, title, content, App.INSTANCE.getString(R.string.lib_tipUtil1_j), onTipSure);
    }

    public static MaterialDialog getSureCancelTip(Activity activity, String title, String content, String sure, OnTipSure onTipSure) {
        MaterialDialog dialog = new MaterialDialog.Builder(activity)
                .customView(R.layout.lib_dialog_sure_cancel, false)
                .autoDismiss(true)
                .cancelable(true)
                .show();
        View customView = dialog.getCustomView();
        TextView tvContent = customView.findViewById(R.id.tvContent);
        TextView tvTitle = customView.findViewById(R.id.tvTitle);
        TextView tvSure = customView.findViewById(R.id.tvSure);
        tvSure.setText(sure);
        tvTitle.setText(title);
        tvContent.setText(content);
        ClickUtil.click(dialog.getCustomView().findViewById(R.id.tvSure), () -> {
            dialog.dismiss();
            onTipSure.onSure();
        });
        ClickUtil.click(dialog.getCustomView().findViewById(R.id.tvCancel), () -> {
            dialog.dismiss();
        });
        return dialog;
    }

    public interface OnTipSure {
        void onSure();

    }

    public interface OnInput {
        void onInput(String... str);

    }

    public interface OnTipSure2 {
        void onSure();

        void onCancel();
    }
}
